import "babel-polyfill";
import * as type from '../action/type';
import {Tool} from '../utils/Tool';
import Util from "../utils/Util";
/**
 * 存储登录的用户信息
 *
 * @param {string} [state=JSON.parse(Tool.localItem('User'))]
 * @param {Object} action
 * @returns Object
 */
const User = (state = JSON.parse(Tool.localItem('User')), action) => {

    switch (action.type) {
        case 'loginSuccess': //登录成功
            Tool.localItem("company",action.UserJson.org_name);
            Tool.localItem('User', JSON.stringify(action.UserJson));
            return action.UserJson;
        case 'loginOut': //退出
            Util.loginOut();
            return null;
        case 'set_user_res':
            return state = JSON.parse(Tool.localItem('User'));
        default:
            return state;
    }
}

const handleData = (state = {isFetching: true, data: {}}, action) => {
    switch (action.type) {
        case type.REQUEST_DATA:
            return {...state, isFetching: true};
        case type.RECEIVE_DATA:
            return {...state, isFetching: false, data: action.data};
        default:
            return {...state};
    }
};
const httpData = (state = {}, action) => {
    switch (action.type) {
        case type.RECEIVE_DATA:
        case type.REQUEST_DATA:
            return {
                ...state,
                [action.category]: handleData(state[action.category], action)
            };
        default:
            return {...state};
    }
};

export default {
    User,httpData
};
